package com.itheima.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.*;
import com.itheima.mapper.*;
import com.itheima.service.db.PmsProductService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.PmsCreateProductVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
@Transactional
public class PmsProductServiceImpl implements PmsProductService {
    @Autowired
    PmsProductMapper pmsProductMapper;
    @Autowired
    PmsMemberPriceMapper pmsMemberPriceMapper;
    @Autowired
    CmsPrefrenceAreaProductRelationMapper cmsPrefrenceAreaProductRelationMapper;
    @Autowired
    PmsProductAttributeValueMapper pmsProductAttributeValueMapper;
    @Autowired
    PmsProductFullReductionMapper pmsProductFullReductionMapper;
    @Autowired
    PmsProductLadderMapper pmsProductLadderMapper;
    @Autowired
    PmsSkuStockMapper pmsSkuStockMapper;
    @Autowired
    CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper;


    //商品信息分页查询
    @Override
    public PageBeanVo findAllByPage(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, Integer productSn, Integer publishStatus, Integer verifyStatus) {

        LambdaQueryWrapper<PmsProduct>lqw=new LambdaQueryWrapper<>();
        lqw.like(brandId!=null,PmsProduct::getBrandId,brandId)
                .like(keyword!=null,PmsProduct::getName,keyword)
                .like(productCategoryId!=null,PmsProduct::getProductCategoryId,productCategoryId)
                .like(productSn!=null,PmsProduct::getProductSn,productSn)
                .like(publishStatus!=null,PmsProduct::getPublishStatus,publishStatus)
                .like(verifyStatus!=null,PmsProduct::getVerifyStatus,verifyStatus)
                .eq(PmsProduct::getDeleteStatus,0);
        Page<PmsProduct> page=new Page<>(pageNum,pageSize);

        Page<PmsProduct> list = pmsProductMapper.selectPage(page, lqw);
        PageBeanVo pageBeanVo = new PageBeanVo(pageNum,pageSize,list.getTotal(),list.getRecords());

        return  pageBeanVo;

    }

    //创建商品
    @Override
    public void createProduct(PmsCreateProductVo pmsCreateProductVo) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(pmsCreateProductVo,pmsProduct);
        //1.添加商品信息
        int productId = pmsProductMapper.insert(pmsProduct);
        //2.
        List<PmsMemberPrice> memberPriceList = pmsCreateProductVo.getMemberPriceList();
        PmsMemberPrice pmsMemberPrice = new PmsMemberPrice();
        BeanUtil.copyProperties(memberPriceList,pmsMemberPrice);
        pmsMemberPrice.setProductId((long) productId);
        pmsMemberPriceMapper.insert(pmsMemberPrice);
        //3.
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = pmsCreateProductVo.getPrefrenceAreaProductRelationList();
        CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation = new CmsPrefrenceAreaProductRelation();
        BeanUtil.copyProperties(prefrenceAreaProductRelationList,cmsPrefrenceAreaProductRelation);
        cmsPrefrenceAreaProductRelation.setProductId((long) productId);
        cmsPrefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
        //4
        List<PmsProductAttributeValue> productAttributeValueList = pmsCreateProductVo.getProductAttributeValueList();
        PmsProductAttributeValue pmsProductAttributeValue = new PmsProductAttributeValue();
        BeanUtil.copyProperties(productAttributeValueList,pmsProductAttributeValue);
        pmsProductAttributeValue .setProductId((long) productId);
        pmsProductAttributeValueMapper.insert(pmsProductAttributeValue);

        //5
        List<PmsProductFullReduction> productFullReductionList = pmsCreateProductVo.getProductFullReductionList();
        PmsProductFullReduction pmsProductFullReduction = new PmsProductFullReduction();
        BeanUtil.copyProperties(productFullReductionList,pmsProductFullReduction);
        pmsProductFullReduction    .setProductId((long) productId);
        pmsProductFullReductionMapper.insert(pmsProductFullReduction);
        //6.
        List<PmsProductLadder> productLadderList = pmsCreateProductVo.getProductLadderList();
        PmsProductLadder pmsProductLadder = new PmsProductLadder();
        BeanUtil.copyProperties(productLadderList,pmsProductLadder);
        pmsProductLadder   .setProductId((long) productId);
        pmsProductLadderMapper.insert(pmsProductLadder);
        //7.
        List<PmsSkuStock> skuStockList = pmsCreateProductVo.getSkuStockList();
        PmsSkuStock pmsSkuStock = new PmsSkuStock();
        BeanUtil.copyProperties(skuStockList,pmsSkuStock);
        pmsSkuStock   .setProductId((long) productId);
        pmsSkuStock.setSkuCode("999999");
        pmsSkuStockMapper.insert(pmsSkuStock);
        //8.
        List<CmsSubjectProductRelation> subjectProductRelationList = pmsCreateProductVo.getSubjectProductRelationList();
        CmsSubjectProductRelation cmsSubjectProductRelation = new CmsSubjectProductRelation();
        BeanUtil.copyProperties(subjectProductRelationList,cmsSubjectProductRelation);
        cmsSubjectProductRelation  .setProductId((long) productId);
        cmsSubjectProductRelationMapper.insert(cmsSubjectProductRelation);

    }

    @Override
    public PmsCreateProductVo findProductInfoById(Integer id) {
        LambdaQueryWrapper<PmsProduct>lqw=new LambdaQueryWrapper<>();
        lqw.eq(PmsProduct::getId,id);
        PmsProduct pmsProduct = pmsProductMapper.selectOne(lqw);
        PmsCreateProductVo pmsCreateProductVo = new PmsCreateProductVo();
        BeanUtil.copyProperties(pmsProduct,pmsCreateProductVo);

        LambdaQueryWrapper<PmsMemberPrice>pmsMemberPriceLambdaQueryWrapper =new LambdaQueryWrapper<>();
        pmsMemberPriceLambdaQueryWrapper.eq(PmsMemberPrice::getProductId,id);
        List<PmsMemberPrice> pmsMemberPrice = pmsMemberPriceMapper.selectList(pmsMemberPriceLambdaQueryWrapper);
        pmsCreateProductVo.setMemberPriceList(pmsMemberPrice);

        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation>cmsPrefrenceAreaProductRelationLambdaQueryWrapper =new LambdaQueryWrapper<>();
        cmsPrefrenceAreaProductRelationLambdaQueryWrapper.eq(CmsPrefrenceAreaProductRelation::getProductId,id);
        List<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelation = cmsPrefrenceAreaProductRelationMapper.selectList(cmsPrefrenceAreaProductRelationLambdaQueryWrapper);
        pmsCreateProductVo.setPrefrenceAreaProductRelationList(cmsPrefrenceAreaProductRelation);

        LambdaQueryWrapper<PmsProductAttributeValue>pmsProductAttributeValueLambdaQueryWrapper =new LambdaQueryWrapper<>();
        pmsProductAttributeValueLambdaQueryWrapper.eq(PmsProductAttributeValue::getProductId,id);
        List<PmsProductAttributeValue> pmsProductAttributeValue = pmsProductAttributeValueMapper.selectList(pmsProductAttributeValueLambdaQueryWrapper);
        pmsCreateProductVo.setProductAttributeValueList(pmsProductAttributeValue);

        LambdaQueryWrapper<PmsProductFullReduction>pmsProductFullReductionLambdaQueryWrapper =new LambdaQueryWrapper<>();
        pmsProductFullReductionLambdaQueryWrapper.eq(PmsProductFullReduction::getProductId,id);
        List<PmsProductFullReduction> pmsProductFullReduction = pmsProductFullReductionMapper.selectList(pmsProductFullReductionLambdaQueryWrapper);
       pmsCreateProductVo.setProductFullReductionList(pmsProductFullReduction);

        LambdaQueryWrapper<PmsProductLadder>pmsProductLadderLambdaQueryWrapper =new LambdaQueryWrapper<>();
        pmsProductLadderLambdaQueryWrapper.eq(PmsProductLadder::getProductId,id);
        List<PmsProductLadder> pmsProductLadder = pmsProductLadderMapper.selectList(pmsProductLadderLambdaQueryWrapper);
        pmsCreateProductVo.setProductLadderList(pmsProductLadder);

        LambdaQueryWrapper<PmsSkuStock>pmsSkuStockLambdaQueryWrapper =new LambdaQueryWrapper<>();
        pmsSkuStockLambdaQueryWrapper.eq(PmsSkuStock::getProductId,id);
        List<PmsSkuStock> pmsSkuStock = pmsSkuStockMapper.selectList(pmsSkuStockLambdaQueryWrapper);
    pmsCreateProductVo.setSkuStockList(pmsSkuStock);

        LambdaQueryWrapper<CmsSubjectProductRelation>cmsSubjectProductRelationLambdaQueryWrapper =new LambdaQueryWrapper<>();
        cmsSubjectProductRelationLambdaQueryWrapper.eq(CmsSubjectProductRelation::getProductId,id);
        List<CmsSubjectProductRelation> cmsSubjectProductRelation = cmsSubjectProductRelationMapper.selectList(cmsSubjectProductRelationLambdaQueryWrapper);
        pmsCreateProductVo.setSubjectProductRelationList(cmsSubjectProductRelation);
    return pmsCreateProductVo;
    }

    //批量上架下架商品,下架0,上架1
    @Override
    public void outProductByIds(List<Integer> ids, Integer publishStatus) {
        ids.forEach(id->{
            LambdaUpdateWrapper<PmsProduct> luw=new LambdaUpdateWrapper<>();
            luw.eq(PmsProduct::getId,id);
            if (publishStatus==0){
                luw.set(PmsProduct::getPublishStatus,0);
                pmsProductMapper.update(null,luw);
            }else {
                luw.set(PmsProduct::getPublishStatus,1);
                pmsProductMapper.update(null,luw);
            }
        });
    }

    @Override
    public void pushProductByIds(List<Integer> ids, Integer recommendStatus) {
        ids.forEach(id->{
            LambdaUpdateWrapper<PmsProduct> luw=new LambdaUpdateWrapper<>();
            luw.eq(PmsProduct::getId,id);
            if (recommendStatus==0){
                luw.set(PmsProduct::getRecommandStatus,0);
                pmsProductMapper.update(null,luw);
            }else {
                luw.set(PmsProduct::getRecommandStatus,1);
                pmsProductMapper.update(null,luw);
            }
        });
    }
    @Override
    public void newProductByIds(List<Integer> ids, Integer newStatus) {
        ids.forEach(id->{
            LambdaUpdateWrapper<PmsProduct> luw=new LambdaUpdateWrapper<>();
            luw.eq(PmsProduct::getId,id);
            if (newStatus==0){
                luw.set(PmsProduct::getNewStatus,0);
                pmsProductMapper.update(null,luw);
            }else {
                luw.set(PmsProduct::getNewStatus,1);
                pmsProductMapper.update(null,luw);
            }
        });
    }
    //批量修改删除状态
    @Override
    public void changeDeleteStatusProductByIds(List<Integer> ids, Integer deleteStatus) {
        ids.forEach(id->{
            LambdaUpdateWrapper<PmsProduct> luw=new LambdaUpdateWrapper<>();
            luw.eq(PmsProduct::getId,id);
            if (deleteStatus==0){
                luw.set(PmsProduct::getDeleteStatus,0);
                pmsProductMapper.update(null,luw);
            }else {
                luw.set(PmsProduct::getDeleteStatus,1);
                pmsProductMapper.update(null,luw);
            }
        });
    }

    @Override
    public void updateProduct(Integer id, PmsCreateProductVo pmsCreateProductVo) {
        LambdaQueryWrapper<PmsProduct> pmsProductLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pmsProductLambdaQueryWrapper.eq(PmsProduct::getId, id);
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(pmsCreateProductVo, pmsProduct);
        pmsProductMapper.update(pmsProduct, pmsProductLambdaQueryWrapper);


        List<PmsMemberPrice> memberPriceList = pmsCreateProductVo.getMemberPriceList();


            PmsMemberPrice pmsMemberPrice = new PmsMemberPrice();
            BeanUtil.copyProperties(memberPriceList, pmsMemberPrice);
            pmsMemberPrice.setProductId(id.longValue());
            pmsMemberPriceMapper.updateById(pmsMemberPrice);


        //3
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = pmsCreateProductVo.getPrefrenceAreaProductRelationList();

            CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation = new CmsPrefrenceAreaProductRelation();
            BeanUtil.copyProperties(prefrenceAreaProductRelationList, cmsPrefrenceAreaProductRelation);
        cmsPrefrenceAreaProductRelation.setProductId(id.longValue());
            cmsPrefrenceAreaProductRelationMapper.updateById(cmsPrefrenceAreaProductRelation);

        //4
        List<PmsProductAttributeValue> productAttributeValueList = pmsCreateProductVo.getProductAttributeValueList();

            PmsProductAttributeValue pmsProductAttributeValue = new PmsProductAttributeValue();
            BeanUtil.copyProperties(productAttributeValueList, pmsProductAttributeValue);
        pmsProductAttributeValue.setProductId(id.longValue());
            pmsProductAttributeValueMapper.updateById(pmsProductAttributeValue);

        //5
        List<PmsProductFullReduction> productFullReductionList = pmsCreateProductVo.getProductFullReductionList();

            PmsProductFullReduction pmsProductFullReduction = new PmsProductFullReduction();
            BeanUtil.copyProperties(productFullReductionList, pmsProductFullReduction);
        pmsProductFullReduction.setProductId(id.longValue());
            pmsProductFullReductionMapper.updateById(pmsProductFullReduction);

        //6
        List<PmsProductLadder> productLadderList = pmsCreateProductVo.getProductLadderList();

            PmsProductLadder pmsProductLadder = new PmsProductLadder();
            BeanUtil.copyProperties(productLadderList, pmsProductLadder);
        pmsProductLadder.setProductId(id.longValue());
            pmsProductLadderMapper.updateById(pmsProductLadder);

        //7
        List<PmsSkuStock> skuStockList = pmsCreateProductVo.getSkuStockList();

            PmsSkuStock pmsSkuStock = new PmsSkuStock();
            BeanUtil.copyProperties(skuStockList, pmsSkuStock);
        pmsSkuStock.setProductId(id.longValue());
            pmsSkuStockMapper.updateById(pmsSkuStock);

        //8

        List<CmsSubjectProductRelation> subjectProductRelationList = pmsCreateProductVo.getSubjectProductRelationList();

            CmsSubjectProductRelation cmsSubjectProductRelation = new CmsSubjectProductRelation();
            BeanUtil.copyProperties(subjectProductRelationList, cmsSubjectProductRelation);
        cmsSubjectProductRelation.setProductId(id.longValue());
            cmsSubjectProductRelationMapper.updateById(cmsSubjectProductRelation);

    }

}
